<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>优惠券列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">

    <link rel="stylesheet" href="/Application/iframe/assets/libs/layuiadmin/layui/css/layui.css"/>
    <link rel="stylesheet" href="/Application/iframe/assets/libs/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="/Application/iframe/assets/libs/layuiadmin/style/mylay.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layui-btn-danger btn-top" data-type="batchdel">批量删除</button>
                <button class="layui-btn btn-top" data-type="add">添加优惠券</button>
            </div>

            <table id="LAY-table" lay-filter="LAY-table"></table>


            <script type="text/html" id="type">
                {{#  if(d.coupon_type == 1){ }}
                <span class="yes_span"> 店铺优惠券 </span>
                {{#  } else { }}
                <span class="yes_span"> 平台优惠券 </span>
                {{#  } }}
            </script>
            <script type="text/html" id="img">
                <img src="{{d.img}}" title="点击放大" lay-event="show">
                <span style="display:none;" id="pic{{d.id}}"><img src="{{d.img}}"></span>
            </script>
            <script type="text/html" id="table-webuser">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail"><i class="layui-icon layui-icon-template-1"></i>查看</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script type="text/javascript" src="/Application/iframe/assets/libs/layuiadmin/layui/layui.js"></script>
<script>
    var url = '/index.php/Merchant/Coupon';
    layui.config({
        base: '/Application/iframe/assets/libs/layuiadmin/'
    }).extend({
        index: 'lib/index'
    }).use(['index', 'table', 'form'], function(){
        var $ = layui.$
            ,form = layui.form
            ,table = layui.table;

        //初始化列表
        table.render({
            elem: '#LAY-table'
            ,url: url + "/CouponLists.html?v=" + new Date().getTime() //接口
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', width:80, title: 'ID',align: "center", sort: true}
                ,{field:'coupon_type', templet:"#type", align: "center", title: '购物券类型'}
                ,{field:'title', align: "center",       title: '购物券名称'}
                ,{field:'img', title: '优惠券图片',align: "center", templet: "#img"}
                ,{field:'start_time', title:'发放时间',align:"center"}
                ,{field:'end_time', title:'结束时间',align:"center"}
                ,{field:'quota', align: "center",   width:100,   title: '购物券数量'}
                ,{field:'take_count',align: "center", width:100, title: '领取数量'}
                ,{field:'used_count', align: "center",width:100, title: '使用数量'}
                ,{field:'update_time', align: "center",title: '修改时间'}
                ,{title: '操作', width:230, align:'center', fixed: 'right', toolbar: '#table-webuser'}
            ]]
            ,page: true
            ,limit: 20
            ,height: 'full-100'
            ,text: {none: '暂无相关数据'}
        });

        //监听编辑+单个删除
        table.on('tool(LAY-table)', function(obj){
            var data = obj.data;
            if(obj.event === 'detail'){
                layer.msg('ID：'+ data.id + ',购物券名称：' + data.title + ',购物券数量：' + data.quota + ',领取数量：'
                    + data.take_count + ',使用数量：' + data.used_count + ' 的查看操作');
            }
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        type:"post",
                        url:url + "/delete.html",
                        data: {id:data.id},
                        dataType:"json",
                        beforeSend: function () {
                            layer.load(1);
                        },
                        success:function(res){
                            layer.closeAll();
                            if(res.code == 1001){
                                obj.del();
                            }
                            else {
                                layer.msg(res.msg);
                            }
                        }
                    });

                });
            } else if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '编辑优惠券'
                    ,content: url + '/edit/id/' + data.id + '.html'
                    ,maxmin: true
                    ,area: ['60%', '60%']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field; //获取提交的字段
                            // var m_show = 0;
                            // if (field.m_show != undefined){
                            //     m_show = field.m_show;
                            // }
                            // if (field.m_min > field.m_max){
                            //     layer.msg('请重新选择满减金额!');
                            //     layer.error()
                            // }
                            if (field.m_max <= 0){
                                layer.msg('最大金额不能为零,或者小于零!');
                                layer.error();
                            }
                            if (field.m_min <= 0){
                                layer.msg('最小金额不能为零,或者小于零!');
                                layer.error();
                            }

                            if (field.m_quota <=0){
                                layer.msg('优惠券数量不能为空!');
                                layer.error()
                            }
                            $.ajax({
                                type:"post",
                                url:url + "/save.html",
                                data: {
                                    id              : field.m_id,          // ID
                                    uid             : field.m_uid,         // uid
                                    coupon_type     : field.m_type,
                                    title           : field.m_title,       // 名称
                                    img             : field.m_pic,         // 图片
                                    with_amount     : field.m_max,         // 满
                                    used_amount     : field.m_min,         // 减
                                    quota           : field.m_quota,       // 优惠券数量
                                    start_time      : field.m_fa_time,     // 发放时间,结束时间
                                    take_count      : field.m_take,        // 已领取数量
                                    used_count      : field.m_used,        // 已使用数量
                                    bz              : field.m_bz,          // 备注/说明
                                },
                                dataType:"json",
                                beforeSend: function () {
                                    layer.load(1);
                                },
                                success:function(res){
                                    layer.close(layer.index);
                                    if(res.code == 1001){
                                        layer.closeAll();
                                        table.reload('LAY-table',{where: {time:new Date()}}); //数据刷新
                                    }
                                    else {
                                        layer.msg(res.msg);
                                    }
                                }
                            });

                        });

                        submit.trigger('click');
                    }
                });
            } else if(obj.event === 'show'){
                layer.open({
                    type: 1,
                    shade: false,
                    title: false,
                    content: $('#pic' + data.id)
                });
            }
        });

        //批量删除+添加
        var active = {
            batchdel: function(){
                var checkStatus = table.checkStatus('LAY-table')
                    ,checkData = checkStatus.data //得到选中的数据
                    ,ids = "0";
                if(checkData.length === 0){
                    return layer.msg('请选择数据');
                }
                $.each(checkData,function(key,value){
                    ids = ids + "," + checkData[key]['id'];
                });
                layer.confirm('确定删除吗？', function(index) {
                    $.ajax({
                        type:"post",
                        url:url + "/delete.html",
                        data: {id:ids},
                        dataType:"json",
                        beforeSend: function () {
                            layer.load(1);
                        },
                        success:function(res){
                            layer.closeAll();
                            if(res.code == 0){
                                table.reload('LAY-table',{where: {time:new Date()}});
                                layer.msg('已删除');
                            }
                            else {
                                layer.msg(res.msg);
                            }
                        }
                    });
                });
            }
            ,add: function(){
                layer.open({
                    type: 2
                    ,title: '添加购物券'
                    ,content: "{:U('Merchant/Coupon/create')}"
                    ,maxmin: true
                    ,area: ['60%', '60%']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field; //获取提交的字段
                            // var m_show = 0;
                            // if (field.m_show != undefined){
                            //     m_show = field.m_show;
                            // }
                            console.log(field.m_min);
                            console.log(field.m_max);
                            if (field.m_max <= 0){
                                layer.msg('最大金额不能为零,或者小于零!');
                                layer.error();
                            }
                            if (field.m_min <= 0){
                                layer.msg('最小金额不能为零,或者小于零!');
                                layer.error();
                            }

                            // if (field.m_min > field.m_max){
                            //     layer.msg('请重新选择满减金额!');
                            //     layer.error()
                            // }
                            if (field.m_quota <=0){
                                layer.msg('优惠券数量不能为空!');
                                layer.error()
                            }

                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                type:"post",
                                url:url + "/save.html",
                                data: {
                                    shop_id         : field.m_shop,        // 店铺Id
                                    m_uid           : field.m_uid,         // uid
                                    title           : field.m_title,       // 优惠券名称
                                    coupon_type     : field.m_type,        // 类型
                                    img             : field.m_pic,         // 图片
                                    with_amount     : field.m_max,         // 满
                                    used_amount     : field.m_min,         // 减
                                    quota           : field.m_quota,       // 优惠券数量
                                    start_time      : field.m_fa_time,     // 发放时间,结束时间
                                    take_count      : field.m_take,        // 已领取数量
                                    used_count      : field.m_used,        // 已使用数量
                                    bz              : field.m_bz,          // 备注/说明
                                    create_time     :field.m_time          // 创建时间

                                },
                                dataType:"json",
                                beforeSend: function () {
                                    layer.load(1);
                                },
                                success:function(res){
                                    layer.close(layer.index);
                                    if(res.code == 1001){
                                        layer.closeAll();
                                        table.reload('LAY-table',{where: {time:new Date()}}); //数据刷新
                                    }
                                    else {
                                        layer.msg(res.msg);
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            }
        };

        $('.layui-btn.btn-top').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>